package myJs.specialMember.viewSample

import myJs.CpLayer
import myJs.Implicits._
import myJs.api.Api
import myJs.common.ViewSampleFormCp
import myJs.cps._
import myJs.myPkg.{MyFetch, ReactRouterDOM, layer}
import myJs.specialMember.sampleList.SampleList
import myJs.specialMember.viewTest.ViewTest
import myJs.tool.Tool
import org.scalajs.dom.{URLSearchParams, html}
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import shared.pojo.Sample.SampleInfo
import slinky.core.FunctionalComponent
import slinky.core.annotations.react
import slinky.core.facade.Hooks.{useEffect, useState}
import slinky.core.facade.React
import slinky.web.html._

/**
 * Created by yz on 21/1/2022
 */
@react object Cp {

  val curPage = ViewSample
  type Props = Unit

  val component = FunctionalComponent[Props] { props =>

    val location = ReactRouterDOM.useLocation()
    val searchParams = new URLSearchParams(location.search)
    val curId = searchParams.get("id")

    val (idV, setIdV) = useState(curId)
    val (sampleInfo, setSampleInfo) = useState(SampleInfo())

    object FTool {

      def refreshSampleInfo = {
        val options = Tool.layerOptions.copy(shade = List("0.1", "#fff"))
        val index = layer.load(0, options)
        Api.sample.getById(idV).map { curSampleInfo =>
          setSampleInfo(curSampleInfo)
          layer.close(index)
        }
      }

    }

    useEffect(() => {
      FTool.refreshSampleInfo
    }, List()
    )

    val formRef = React.createRef[html.Form]

    CpLayer(title = curPage.titleStr)(
      div(className := "page-content",
        div(className := "row-fluid",
          div(className := "row",
            div(className := "col-md-12 col-sm-12",
              div(className := "portlet light",
                div(className := "portlet-title",
                  div(className := "item",
                    div(className := "title clear",
                      a(href := s"#${SampleList.absolutePath}", SampleList.titleStr), " / ",
                      span(curPage.titleStr)
                    )
                  ),
                ),
                div(className := "portlet-body",

                  ViewSampleFormCp(labFileShow = false),

                ),
              )
            ),

          ),

        )
      )
    )

  }


}
